#!/bin/bash

export PATH=/bin:/usr/bin:/usr/sbin

if [ $# -eq 0 -o $# -gt 2 ]; then
	echo -e "Usage: $(basename $0) -a|-s|\"host[ host] ...\" [file1[,file2...]]"
	exit 
fi

files=""

if [ "$1" == "-a" ]; then
		servers="all"
else
	if [ "$1" == "-s" ]; then
			servers="secondaries"
	else
		servers=$1
		host $servers >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "Error: $servers cannot be resolved."
			echo -e "Usage: $(basename $0) -a|-s|\"host[ host] ...\" [file1 [file2...]]"
			exit 1
		fi
	fi
fi

if [ "X$2" != "X" ]; then
	files="$(echo $2 | sed -e 's/,/ /g')"
fi

if [ ! -f $(dirname $0)/IT_DNS_SERVERS_PHYSICAL ]; then
	echo "Error: $(dirname $0)/IT_DNS_SERVERS_PHYICAL is missing."
	exit 1
fi

. $(dirname $0)/IT_DNS_SERVERS_PHYSICAL

if [ "$servers" == "all" ]; then
	servers="$(echo $PRIMARY $SECONDARY)"
else
	if [ $servers == "secondaries" ]; then
		servers="$(echo $SECONDARY)"
	fi
fi

for s in $(echo $servers); do
	echo "Copying tools to ${s}..."
	if [ "X${files}" != "X" ]; then
		scp -p $files ${s}:/usr/local/sbin
	else
		scp -p [a-z]* ${s}:/usr/local/sbin
	fi
done
